﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Modules_ModifiyUserInfor : System.Web.UI.UserControl
{
    private MysqlHelp CheckUser = new MysqlHelp();
    private DataTable CheckTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AsUserName"] != null && Session["AsUserName"].ToString() != "")
        {
            oldpass.Visible = true;
            CheckTable = CheckUser.GetDatabysql("select * from user where username='" + Session["AsUserName"].ToString() + "'");
            TextBox_NewQQ.Text = CheckTable.Rows[0]["qq"].ToString();
            TextBox_NewProblem.Text = CheckTable.Rows[0]["problem"].ToString();
            TextBox_NewAnswer.Text = CheckTable.Rows[0]["answer"].ToString();
            TextBox_NewEmail.Text = CheckTable.Rows[0]["email"].ToString();
            TextBox_NewPhone.Text = CheckTable.Rows[0]["telphone"].ToString();
            MultiView_ModifiyUser.SetActiveView(View_Loged);           
        }
        else
        {
            if (Label_HideUserName.Text == "")
            {
                MultiView_ModifiyUser.SetActiveView(View_UnLog);
            }
            else
            {
                CheckTable = CheckUser.GetDatabysql("select * from user where username='" + Label_HideUserName.Text + "'");
                MultiView_ModifiyUser.SetActiveView(View_Loged);
                TextBox_NewQQ.Text = CheckTable.Rows[0]["qq"].ToString();
                TextBox_NewProblem.Text = CheckTable.Rows[0]["problem"].ToString();
                TextBox_NewAnswer.Text = CheckTable.Rows[0]["answer"].ToString();
                TextBox_NewEmail.Text = CheckTable.Rows[0]["email"].ToString();
                TextBox_NewPhone.Text = CheckTable.Rows[0]["telphone"].ToString();
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckTable.Clear();
        CheckTable = CheckUser.GetDatabysql("select * from user where username='" + TextBox_InUser.Text + "'");
        if (CheckTable.Rows.Count!=0)
        {
            Check.Visible = true;
            VerifiyUser.Visible = false;
              Label_Problem.Text=CheckTable.Rows[0]["problem"].ToString();
            Page_Load(null, null);
        }
        else
        {
            Response.Write("<script>alert('无此用户');history.go(-1);</script>");
        }
    }
    protected void Button_ProblemSb_Click(object sender, EventArgs e)
    {
        CheckTable.Clear();
        CheckTable = CheckUser.GetDatabysql("select * from user where username='" + TextBox_InUser.Text + "'");
        if (TextBox_Answer.Text==CheckTable.Rows[0]["answer"].ToString())
        {
             Label_HideUserName.Text = CheckTable.Rows[0]["username"].ToString();
            Page_Load(null, null);
        }
        else
        {
            Response.Write("<script>alert('输入答案错误');</script>");
        }
    }
    protected void Button_ModifySub_Click(object sender, EventArgs e)
    {
        string NewPassWord = TextBox_NewPassWord.Text.Trim();
        string OldPassWord = TextBox_OldPassWord.Text.Trim();
        string NewEmail = TextBox_NewEmail.Text.Trim();
        string NewAnswer = TextBox_NewAnswer.Text.Trim();
        string NewProblem = TextBox_NewProblem.Text.Trim();
        string NewPhone = TextBox_NewPhone.Text.Trim();
        string NewQQ = TextBox_NewQQ.Text.Trim();
        string ClUserName;
        if(Session["AsUserName"]!=null)
            ClUserName=Session["AsUserName"].ToString();
        else
            ClUserName=TextBox_InUser.Text;
        AsCheck AsUserCheck = new AsCheck();
        if (NewPassWord != "" && NewEmail != "" && NewAnswer != "" && NewProblem != "" && NewPhone != "" && NewQQ != "")
        {
            if (AsUserCheck.IsShortLen(NewPassWord, 6) == true)
            {
                Response.Write("<script>alert('密码太短，最小6位！');history.go(-1);</script>");
            }
            else
            {
                if (oldpass.Visible == true)
                {
                    CheckTable = CheckUser.GetDatabysql("select * from user where username='" + ClUserName + "'");
                    if(OldPassWord!=CheckTable.Rows[0]["password"].ToString())
                    Response.Write("<script>alert('旧密码错误！');history.go(-1);</script>");
                }
                    if (AsUserCheck.IsValidEmail(NewEmail) == false)
                    {
                        Response.Write("<script>alert('邮箱不合法！');history.go(-1);</script>");
                    }
                    else
                    {
                        MysqlHelp sqlreg = new MysqlHelp();
                        string sqlstr = "update user set password='"+NewPassWord+"',email='"+NewEmail+"',qq='"+NewQQ+"',telphone='"+NewPhone+"',problem='"+NewProblem+"',answer='"+NewAnswer+"'where username='"+ClUserName+"'";
                        int back = sqlreg.Runsql(sqlstr);
                        if (back != -1)
                        {
                            Response.Write("<script>alert('信息修改成功！');</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('信息修改失败！');history.go(-1);</script>");
                        }
                    }
            }
        }
        else
        {
            Response.Write("<script>alert('请填写完整的信息！');history.go(-1);</script>");
        }
    }
}
